<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/resources/accesskey.js"></script>

<p>Tests that a keyboard access key to press a button will fire only the click event</p>
<button id="button" accesskey="g">Click Me with Shift+Alt+g or on Mac with Control+Option+g</button>
<input id="inputbutton" type="button" accesskey="b" value="Click me with Shift+Alt+b or on Mac with Control+Option+b">
<label id="label" accesskey="l">Click Me with Shift+Alt+l or on Mac with Control+Option+l</label>

<script>
let button = document.getElementById("button");
let inputbutton = document.getElementById("inputbutton");
let radiobutton = document.getElementById("radiobutton");
let label = document.getElementById("label");
let elementList = [button, inputbutton, label];
let eventLog = [];
const eventList = ["pointerdown", "pointerup", "mousedown", "mouseup", "click"];
elementList.forEach((el)=>{eventList.forEach((ev)=>el.addEventListener(ev, (e)=>{
  eventLog.push(`${ev}_${el.id}`);
  if(ev === "click" && currentTest){
    currentTest.step(()=>{
      if(e instanceof PointerEvent){
        // We want the test to run on all browsers even if click is not a PointerEvent.
        assert_equals(e.pointerId, -1, "Click's pointerId has -1 because of non-pointing device input");
        assert_equals(e.pointerType, "", "Click's pointerType has default value");
      }
      assert_equals(e.screenX, 0, "Click's screenX has default value");
      assert_equals(e.screenY, 0, "Click's screenY has default value");
      assert_equals(e.clientX, 0, "Click's clientX has default value");
      assert_equals(e.clientY, 0, "Click's clientY has default value");
      assert_equals(e.pageX, 0, "Click's pageX has default value");
      assert_equals(e.pageY, 0, "Click's pageY has default value");
      // https://github.com/w3c/csswg-drafts/issues/1070
      assert_equals(e.offsetX, 0, "Click's offsetX should have default value");
      assert_equals(e.offsetY, 0, "Click's offsetY should have default value");
    });
  }
}));});
let currentTest;
let testElements = [button, inputbutton, label];
let accesskeyMap = new Map([[button, "g"], [inputbutton, "b"], [label, "l"]]);
testElements.forEach((el)=>promise_test((test)=> new Promise(async (resolve,reject)=>{
  currentTest = test;
  eventLog = [];
  var eventWatcher = new EventWatcher(test, el, ['click']);
  let waitForClick = eventWatcher.wait_for('click');
  await pressAccessKey(accesskeyMap.get(el));
  await waitForClick;

  assert_array_equals(eventLog, [`click_${el.id}`], "The Keyboard generated click only sends the click event.");
  resolve();
}), `Test that the Keyboard generated click does not fire pointer or mouse events for ${el.id}`));
</script>
